import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
import tensorflow.keras as keras
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# 定义数据
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)


# 定义模型
model = keras.Sequential()
model.add(layers.Dense(64,activation='relu',input_dim=20))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(64,activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10,activation='softmax'))

# 定义模型编译
sgd = keras.optimizers.SGD(lr=0.01,decay=1e-6,momentum=0.9, nesterov=True)
model.compile(optimizer=sgd,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# 模型训练
model.fit(x_train,y_train,batch_size=128,epochs=20)
# 模型预测
score = model.evaluate(x_test,y_test,batch_size=128)

print(score)






